package com.csdj.wenjun.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.csdj.wenjun.common.CacheConstant;
import com.csdj.wenjun.common.WebResult;
import com.csdj.wenjun.common.WebResultMsgCode;
import com.csdj.wenjun.entity.Jifenjilu;
import com.csdj.wenjun.entity.User;
import com.csdj.wenjun.service.JifenjiluService;
import com.csdj.wenjun.service.RedisService;

import com.csdj.wenjun.service.UserService;
import com.csdj.wenjun.utils.EncryptUtil;
import com.csdj.wenjun.utils.NewTime;
import com.csdj.wenjun.utils.QQyouxiang;
import com.csdj.wenjun.utils.RandomUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wenjun
 * @since 2022-01-04
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private RedisService redisService;
    @Autowired
    QQyouxiang qQyouxiang;
    @Autowired
    UserService userService;
    @Autowired
    private JifenjiluService jifenjiluService;
    @GetMapping("/yzm")
    public WebResult selectYzm(@RequestParam String email){
        String yzm= RandomUtil.getUUID(8);
        if(qQyouxiang.sendMail(email,"验证码",yzm)){
            redisService.setString(CacheConstant.EMAIL+email,yzm,60*15);
            return new WebResult(WebResultMsgCode.SUCCESS,yzm);
        }
        return new WebResult(WebResultMsgCode.USER_PWD_ERROR);
    }
    @GetMapping("/zhuche")
    public WebResult zhuche(@RequestParam String email,@RequestParam String password,@RequestParam String yzm,@RequestParam String yqm){
        User user=new User();
        user.setUsername(email);
        user.setHearurl("http://localhost:8082/wenjun/img/1637495830382.png");
        user.setPassword(EncryptUtil.getMd5(password));
        user.setJifen(10);
        if(yqm==null){
            user.setTopid("0");
        }else {
            user.setTopid(yqm);
        }
        String toyzm = redisService.getString(CacheConstant.EMAIL+email);
        while (true){
            String yzm1= RandomUtil.getYQM(8);
            QueryWrapper<User> queryWrapper=new QueryWrapper<>();
            queryWrapper.lambda().eq(User::getYqm,yzm1);
            if(userService.getOne(queryWrapper)==null){
                user.setYqm(yzm1);
                break;
            }
        }
        if (yzm.equals(toyzm)){
            QueryWrapper<User> queryWrapper=new QueryWrapper<>();
            queryWrapper.lambda().eq(User::getUsername,email);
            if(userService.save(user)){
                User user1=userService.getOne(queryWrapper);
                Jifenjilu jifenjilu=new Jifenjilu();
                jifenjilu.setShuliang(10);
                jifenjilu.setType("注册账号");
                jifenjilu.setUserid(user1.getId());
                jifenjilu.setYujifen(10);
                jifenjiluService.save(jifenjilu);
                if(yqm.equals("0")){
                    QueryWrapper<User> queryWrapper1=new QueryWrapper<>();
                    queryWrapper1.lambda().eq(User::getYqm,yqm);
                    User user2=userService.getOne(queryWrapper);
                    if(user2!=null){
                        UpdateWrapper updateWrapper = new UpdateWrapper();
                        updateWrapper.eq("id", user2.getId());
                        updateWrapper.set("jifen",user2.getJifen()+20);
                        Jifenjilu jifenjilu1=new Jifenjilu();
                        jifenjilu1.setShuliang(20);
                        jifenjilu1.setType("邀请新用户");
                        jifenjilu1.setUserid(user2.getId());
                        jifenjilu1.setYujifen(user2.getJifen()+20);
                        jifenjiluService.save(jifenjilu1);
                        if(userService.update(updateWrapper)){
                            System.out.println("老用户"+user2.getUsername()+"邀请成功");
                            diguiyaoqing(user.getTopid());
                        }
                    }
                }
                return new WebResult(WebResultMsgCode.SUCCESS,true);
            }
        }
        return new WebResult(WebResultMsgCode.SUCCESS,false);
    }
    public void diguiyaoqing(String topid){
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(User::getTopid,topid);
        User user2=userService.getOne(queryWrapper);
        Jifenjilu jifenjilu=new Jifenjilu();
        jifenjilu.setShuliang(10);
        jifenjilu.setType("下级拉新");
        jifenjilu.setUserid(user2.getId());
        jifenjilu.setYujifen(user2.getJifen()+10);
        jifenjiluService.save(jifenjilu);
        if(user2.getTopid().equals("0")){
            return;
        }
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id", user2.getId());
        updateWrapper.set("jifen",user2.getJifen()+10);
        if(userService.update(updateWrapper)){
            System.out.println("老用户"+user2.getUsername()+"邀请成功");
            diguiyaoqing(user2.getTopid());
        }
    }
    @GetMapping("/login")
    public WebResult login(@RequestParam String username,@RequestParam String password){
        if(redisService.getString(CacheConstant.EMAIL+username)==null){
            redisService.setObject(CacheConstant.EMAIL+username,0, (int) NewTime.shengYuTime());
        }
        if(redisService.getString(CacheConstant.EMAIL+username).equals("6")){
            return new WebResult(WebResultMsgCode.DENGLUCISHUCHAOSHI);
        }
        User user = userService.login(username, password);
        if(user != null){
            return new WebResult(WebResultMsgCode.SUCCESS,user);
        }
        redisService.incr(CacheConstant.EMAIL+username);
        return new WebResult(WebResultMsgCode.USER_PWD_ERROR);
    }
    @GetMapping("/selectEmail")
    public WebResult selectEmail(@RequestParam String email){
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(User::getUsername,email);
        User user = userService.getOne(queryWrapper);
        if(user != null){
            return new WebResult(WebResultMsgCode.SUCCESS,user);
        }
        return new WebResult(WebResultMsgCode.USER_PWD_ERROR);
    }
    @GetMapping("/selectuserid")
    public User selectuserid(@RequestParam String id){
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(User::getId,id);
        return userService.getOne(queryWrapper);
    }
    @GetMapping("/updateusertu")
    public User updateusertu(@RequestParam String id,@RequestParam String tupian){
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id", id);
        updateWrapper.set("hearurl",tupian);
        if(userService.update(updateWrapper)){
            System.out.println("修改头像成功");
        }else {
            System.out.println("修改失败");
        }
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(User::getId,id);
        return userService.getOne(queryWrapper);
    }
    @GetMapping("/updateusername")
    public int updateusername(@RequestParam String id,@RequestParam String name){
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id", id);
        updateWrapper.set("name",name);
        if(userService.update(updateWrapper)){
            System.out.println("修改名字成功");
        }else {
            System.out.println("修改失败");
            return 0;
        }
        return 1;
    }
    @GetMapping("/suoyouuser")
    public List suoyouuser(){
        return userService.list();
    }
    @GetMapping("/updateusercontext")
    public int updateusercontext(@RequestParam String id,@RequestParam String context){
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id", id);
        updateWrapper.set("context",context);
        if(userService.update(updateWrapper)){
            System.out.println("修改简介成功");
        }else {
            System.out.println("修改失败");
            return 0;
        }
        return 1;
    }
    @GetMapping("/test")
    public Page test(@RequestParam int id, @RequestParam int yeshu){
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(User::getId,id).orderByDesc(true,User::getId);
        Page<User> producePage = new Page<>(yeshu,5);
        Page<User> page = userService.page(producePage,queryWrapper);
        System.out.println(producePage == page);
        List<User> records = page.getRecords();
        System.out.println(page.getPages());
        System.out.println(page.getTotal());
        for (User record : records) {
            System.out.println(record);
        }
        return page;
    }
}
